package org.yeasy.es.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ElasticSearchConfig {

    @Value("${heima.elasticsearch.hostlist}")
    private String hostlist;

    @Bean(destroyMethod = "close")
    public RestHighLevelClient restHighLevelClient(){
        // 解析host list
        String[] split = hostlist.split(",");

        HttpHost[] hosts=new HttpHost[split.length];
        for (int i=0;i<hosts.length;i++) {
            String[] split1 = split[i].split(":");
            HttpHost httpHost = new HttpHost(split1[0], Integer.parseInt(split1[1]), "http");
            hosts[i]=httpHost;
        }

        RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(hosts));
        return client;
    }
}
